# Natural Language Toolkit: Alignment Interface
#
# Copyright (C) 2001-2010 NLTK Project
# URL: <http://www.nltk.org/>
# For license information, see LICENSE.TXT

"""
Storage and processing classes for AlignedSent in corpus
"""


from aligned_sent import AlignedSent

class AlignedSentCollection(object):
    """
    the collection of AlignedSents. contains the corpus and keeps track
    of words appeared in the corpus in both english and foreign language.
    """
    
    def __init__(self, aligned_sents = []):
        """
        Initialize a new C{AlignedSentCollection}.

        @param aligned_sents: the list of AlignedSent objects
        @type aligned_sents: C{list} of C{AlignedSent}
        """
        self.set_words = set([])
        self.set_mots = set([])
        self.sents = aligned_sents[:]
        self.refresh_vocabulary()

    def __repr__(self):
        return repr(self.sents)
    
    def refresh_vocabulary(self):
        """
        Update the set of english and foreign words
        """
        e = set([])
        f = set([])
        for x in self.sents:
            for i in x._words:
                e.update([i])
            for j in x._mots:
                f.update([j])
        self.set_words = e
        self.set_mots = f
        
    def add(self, aligned_sent):
        """
        add a new AlignedSent instance into collection

        @param aligned_sent: the AlignedSent object to be added
        @type aligned_sent: C{AlignedSent}
        """
        self.sents.append(aligned_sent)
        self.refresh_vocabulary()
        
    def remove(self, aligned_sent):
        """
        remove a AlignedSent form collection

        @param aligned_sent: the AlignedSent instance to be removed
        @type aligned_sent: C{AlignedSent}
        """
        self.sents = [i for i in self.sents if i != aligned_sent]
        self.refresh_vocabulary()
        
